<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="renderer" content="webkit" />
    <title>Melon后台</title>
    <link rel="icon" href="/assets/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.6.5/css/layui.css">
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="/assets/css/zadmin.css" media="all">
    <link rel="stylesheet" href="/assets/css/login.css">
</head>
<body class="layui-layout-login">
<div class="login-bg">
    <div class="cover"></div>
</div>
<div class="login-content">
    <h1 class="login-box-title">
        <i class="fa fa-fw fa-user"></i>登录
    </h1>
    <form id="login-form" class="layui-form" method="post" onsubmit="return false;">
        <div class="layui-form-item">
            <label class="layui-icon layui-icon-username" for="username"></label>
            <input class="layui-input" type="text" name="username" id="username" placeholder="用户名" lay-verify="required" lay-vertype="tips">
        </div>
        <div class="layui-form-item">
            <label class="layui-icon layui-icon-password" for="password"></label>
            <input class="layui-input" type="password" name="password" id="password" placeholder="密码" lay-verify="required" lay-vertype="tips">
        </div>
        <div class="layui-form-item">
            <input type="checkbox" name="remember-me" title="记住我" lay-skin="primary" id="remember-me"> <a
                class="layui-layout-right forget-password" href="javascript:alert('请联系超级管理员！')">忘记密码?</a>
        </div>
        <button lay-submit lay-filter="login" class="layui-btn layui-btn-fluid ajax-login" onclick="login(this)">
            <i class="fa fa-sign-in fa-lg fa-fw"></i> 登录
        </button>
    </form>
</div>
<!-- jQuery 3 -->
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://www.layuicdn.com/layui-v2.6.5/layui.js"></script>

<script type="text/javascript">
    layui.use([ 'element', 'form', 'upload' ], function() {
        var table = layui.table
            , $ = layui.$
            , _upload = layui.upload
            , element = layui.element
            , form = layui.form;
    });
    if (top !== self) {
        parent.location.href = '/loginPage';
    }

    var token = localStorage.getItem("token");
    if (token != null && token.trim().length !== 0) {
        $.ajax({
            type : 'get',
            url : '/user/current?token=' + token,
            success : function(data) {
                location.href = '/main/index';
            },
            error : function(xhr, textStatus, errorThrown) {
                var msg = xhr.responseText;
                var response = JSON.parse(msg);
                var code = response.code;
                var message = response.message;
                if (code === 401) {
                    localStorage.removeItem("token");
                }
            }
        });
    }

    function login(obj) {
        $(obj).attr("disabled", true);

        var username = $.trim($('#username').val());
        var password = $.trim($('#password').val());
        var jsonData = {'username': username, 'password': password}
        if (username === "" || password === "") {
            $("#info").html('用户名或者密码不能为空');
            $(obj).attr("disabled", false);
        } else {
            $.ajax({
                type : 'post',
                url : '/login',
                data : JSON.stringify(jsonData),
                success : function(data) {
                    var dataObj = data.data;
                    localStorage.setItem("token", dataObj.token);
                    location.href = '/main/index';
                },
                error : function(xhr, textStatus, errorThrown) {
                    var responseText = xhr.responseText;
                    var response = JSON.parse(responseText);
                    var code = response.code;
                    var message = response.message;
                    layui.use('layer', function(){
                        var layer= layui.layer;
                        layer.alert(response.message, {icon: 6});
                    });
                    $(obj).attr("disabled", false);
                }
            });
        }
    }
</script>
</body>
</html>